package com.hxd.otc.module.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hxd.otc.R;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.module.home.fragment.HomePageFragment;
import com.hxd.otc.module.my.activity.WebViewActivity;
import com.hxd.otc.module.my.adapter.FniancialAdapter;
import com.hxd.otc.module.my.contract.FinacialContract;
import com.hxd.otc.module.my.presenter.FinacialPresenter;
import com.hxd.otc.module.order.bean.FinacialBean;
import com.hxd.otc.utils.DataUtils;

import butterknife.BindView;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/12/26 15:07
 * 描  述 :  ${}
 */
public class FinancialStatementsFragment extends LazyFragment<FinacialPresenter> implements FinacialContract.View {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    View errorView;

    View notDataView;

    TextView tvRefresh;

    FniancialAdapter mAdapter;

    private boolean isRefresh = true;
    private int currentPage = 1;//当前页
    private int rows = 50;//每页显示条数

    @Override
    protected void initData() {
        recyclerView.setNestedScrollingEnabled(false);
        initAdapter();
        refreshData();
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));


        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) recyclerView.getParent(), false);
        notDataView.setPadding(0, DataUtils.dp2px(getContext(), 40), 0, DataUtils.dp2px(getContext(), 40));
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) recyclerView.getParent(), false);
        errorView.setPadding(0, DataUtils.dp2px(getContext(), 40), 0, DataUtils.dp2px(getContext(), 40));
        tvRefresh = errorView.findViewById(R.id.tv_refresh);
        tvRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshData();
            }
        });
    }

    public void refreshData() {
        isRefresh = true;
        currentPage = 1;
        mPresenter.doLoadData(rows, currentPage);
    }

    public void loadMoreData() {
        isRefresh = false;
        mPresenter.doLoadData(rows, ++currentPage);
    }

    private void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new FniancialAdapter();
            mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putString(WebViewActivity.EXTRA_TITLE, mAdapter.getData().get(position).name);
                    bundle.putString(WebViewActivity.EXTRA_URL, mAdapter.getData().get(position).url);
                    bundle.putBoolean(WebViewActivity.EXTRA_IS_PDF, true);
                    bundle.putBoolean(WebViewActivity.EXTRA_IS_HELP_CENTER, false);
                    intent.putExtras(bundle);
                    intent.setClass(getContext(), WebViewActivity.class);
                    startActivity(intent);
                }
            });
        }
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_stable_currency;
    }

    @Override
    protected FinacialPresenter createPresenter() {
        return new FinacialPresenter(this);
    }

    @Override
    public void onShowNoMore() {
        ((HomePageFragment) getParentFragment()).onShowNoMore();
    }

    @Override
    public void onSetAdapter(FinacialBean bean) {
        if (isRefresh) {
            if (bean != null && bean.list.size() <= 0) {
                mAdapter.setEmptyView(notDataView);
            }else {
                mAdapter.setNewData(bean.list);
            }
        } else {
            mAdapter.addData(bean.list);
        }

    }

    @Override
    public void onError(int code) {
        super.onError(code);
        onShowNoMore();
        if (isRefresh)
            mAdapter.setEmptyView(errorView);
    }
}
